package edu.dali.enterprise_test.service.auth.impl;

import edu.dali.enterprise_test.dao.mapper.auth.AuthenticationMapper;
import edu.dali.enterprise_test.model.Pager;
import edu.dali.enterprise_test.model.vo.Dto;
import edu.dali.enterprise_test.model.vo.SysPermission;
import edu.dali.enterprise_test.model.vo.SysRole;
import edu.dali.enterprise_test.model.vo.UserInfo;
import edu.dali.enterprise_test.service.auth.AuthService;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Random;

@Service
@Transactional
public class AuthServiceImpl implements AuthService {

    @Autowired
    private AuthenticationMapper authenticationMapper;
    @Override
    public UserInfo selectUser(Dto dto) {

        UserInfo user = authenticationMapper.selectUser(dto);
        if (user == null)
            return null;

        Dto rDto = new Dto();
        rDto.put("uid" , user.getUid());
        List<SysRole> roleList = authenticationMapper.selectUserRoles(rDto);
        for (SysRole sr : roleList) {
            Dto pDto = new Dto();
            pDto.put("roleid" , sr.getId());
            List<SysPermission> permissions = authenticationMapper.selectRolePermission(pDto);
            sr.setPermissions(permissions);
        }
        user.setRoles(roleList);

        return user;
    }

    @Override
    public Pager getUserInfoByPage(Dto dto) {

        List<UserInfo> userInfos = authenticationMapper.queryUser(dto);
        int total = authenticationMapper.queryUserCount(dto);
        Pager p = new Pager();
        p.setCode(0);
        p.setCount(total);
        p.setData(userInfos);
        p.setMsg("ok");
        return p;
    }

    @Override
    public void addUser(UserInfo user) {
        String password = "123456";
        if (user.getPassword() != null && !"".equals(user.getPassword())) {
            password = user.getPassword();
        }
        Random random = new Random();
        String salt = String.valueOf(random.nextInt(10000));
        salt = new Md5Hash(salt).toString();
        user.setSalt(salt);
        String pwd = new Md5Hash(password, salt , 2).toString();
        user.setPassword(pwd);

        authenticationMapper.insertUserInfo(user);
        List<SysRole> rlist = user.getRoles();
        for(SysRole role : rlist) {
            Dto rDto = new Dto();
            rDto.put("uid" , user.getUid());
            rDto.put("roleId" , role.getId());
            authenticationMapper.insertUserRole(rDto);
        }
    }

    @Override
    public void updateUser(UserInfo user) {
        authenticationMapper.updateByUid(user);

        Dto dto = new Dto();
        dto.put("uid" , user.getUid());
        authenticationMapper.deleteUserRole(dto);
        List<SysRole> rlist = user.getRoles();
        for(SysRole role : rlist) {
            Dto rDto = new Dto();
            rDto.put("uid" , user.getUid());
            rDto.put("roleId" , role.getId());
            authenticationMapper.insertUserRole(rDto);
        }
    }

    @Override
    public void deleteUser(Long uid) {
        authenticationMapper.deleteByUid(uid);
    }

    @Override
    public List<SysRole> getAllRoles(Dto dto) {
        return authenticationMapper.getAllRoles(dto);
    }

    @Override
    public List<SysPermission> getAllPermissions(Dto dto) {
        return authenticationMapper.getAllPermission(dto);
    }

    @Override
    public void updatePwd(String uid, String upwd) {
        Dto dto = new Dto();
        dto.put("uid" , uid);
        UserInfo user = selectUser(dto);

        String password = upwd;
//        if (user.getPassword() != null && !"".equals(user.getPassword())) {
//            password = user.getPassword();
//        }
        Random random = new Random();
        String salt = String.valueOf(random.nextInt(10000));
        salt = new Md5Hash(salt).toString();
        user.setSalt(salt);
        String pwd = new Md5Hash(password, salt , 2).toString();
        user.setPassword(pwd);

        authenticationMapper.updateByUid(user);
    }


}
